//
//  StagePrototypeParser.cpp
//  airfight
//
//  Created by aaron more on 12-9-12.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>
#include "StagePrototypeParser.h"

StagePrototypeParser::StagePrototypeParser()
:prototypes(NULL)
,currentStage(NULL)
,currentEnemy(NULL)
{
    enemyIdx = 10;
}

StagePrototypeParser::~StagePrototypeParser()
{
    CC_SAFE_RELEASE(currentStage);
    CC_SAFE_RELEASE(prototypes);
}

StagePrototypeParser * StagePrototypeParser::formatWithXMLFile(const char *xmlFile)
{
    StagePrototypeParser *pRet = new StagePrototypeParser();
    if(pRet->initWithXMLFile(xmlFile))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

void StagePrototypeParser::internalInit(const char* tmxFileName)
{
    prototypes = StagePrototypes::getInstance();
    prototypes->retain();
    if (tmxFileName != NULL)
    {
        m_sXMLFileName = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(tmxFileName);
    }
}

void StagePrototypeParser::startElement(void *ctx, const char *name, const char **atts)
{
    
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
    if(atts && atts[0])
    {
        for(int i = 0; atts[i]; i += 2) 
        {
            std::string key = (char*)atts[i];
            std::string value = (char*)atts[i+1];
            attributeDict->insert(std::pair<std::string, std::string>(key, value));
        }
    }
    if("stage" == elementName )
    {
        Stage *stage = new Stage();
        //        int i = 0;
        //        sscanf(valueForKey("id", attributeDict), "%d", &i);
        stage->setStageID((int)atoi(valueForKey("id", attributeDict)));
        stage->setName(valueForKey("name", attributeDict));
        stage->setDistance((int)atoi(valueForKey("distance", attributeDict)));
        if (!currentStage) 
        {
            currentStage = stage;
        }
    }
    else if("front" == elementName) 
    {
        if(currentStage)
        {
            Background *background = new Background();
            background->setSpeed((int)atoi(valueForKey("speed", attributeDict)));
            background->setRes(valueForKey("res", attributeDict));
            currentStage->setFrontBackground(background);
            background->release();  
        }
    }
    else if("rear" == elementName)
    {
        if(currentStage)
        {
            Background *background = new Background();
            background->setSpeed((int)atoi(valueForKey("speed", attributeDict)));
            background->setRes(valueForKey("res", attributeDict));
            currentStage->setRearBackground(background);
            background->release();  
        }
    }
    else if("enemy" == elementName)
    {
        StageEnemy *enemy = new StageEnemy();
        enemy->setEnemyID((int)atoi(valueForKey("id", attributeDict)));
        enemy->setEnterPos((int)atoi(valueForKey("enterX", attributeDict)), (int)atoi(valueForKey("enterY", attributeDict)));
        enemy->setStartY((int)atoi(valueForKey("start", attributeDict)));
        enemy->setTraceID((int)atoi(valueForKey("traceid", attributeDict)));
        enemy->setBonusID((int)atoi(valueForKey("bonusid", attributeDict)));
        std::map<std::string, std::string>::iterator it = attributeDict->find("isboss");
        if(it == attributeDict->end())
        {
            enemy->setIsBoss(0);
        }
        else
        {
            enemy->setIsBoss((int)atoi(valueForKey("isboss", attributeDict)));
        }
        
        enemy->setEnemyTag(++enemyIdx);
        if(currentStage && !currentEnemy)
        {
            currentEnemy = enemy;
            
        }
    }
}

void StagePrototypeParser::endElement(void *ctx, const char *name)
{
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    if("stage" == elementName)
    {
        
        prototypes->addStage(currentStage);
        currentStage->release();
        currentStage = NULL;
    }
    else if("enemy" == elementName)
    {
        currentStage->addStartEnemyID(currentEnemy);
        currentEnemy->release();
        currentEnemy = NULL;
    }
}

void StagePrototypeParser::textHandler(void *ctx, const char *s, int len)
{
    
}